//
//  DCLocationManager.m
//  DCProject
//
//  Created by bigbing on 2019/11/25.
//  Copyright © 2019 bigbing. All rights reserved.
//

#import "DCLocationManager.h"
#import <CoreLocation/CoreLocation.h>
#import "DCAlterView.h"

@interface DCLocationManager ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager * locationManager;

@end

@implementation DCLocationManager

+ (DCLocationManager *)shareManager
{
    static DCLocationManager * _instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[[self class] alloc] init];
    });
    return _instance;
}



- (void)dc_location
{
    if ([CLLocationManager locationServicesEnabled]) { // 允许定位
        
        self.locationManager = nil;
        self.locationManager.delegate = nil;
        
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager startUpdatingLocation];
        
    } else {
        
        DCAlterView * alterView = [[DCAlterView alloc] initWithTitle:@"提示" content:@"定位失败，请确定您的定位权限是否开启"];
        [alterView addActionWithTitle:@"确认" type:DCAlterTypeDone halderBlock:^(UIButton *button) {
            if (DC_CanOpenUrl(UIApplicationOpenSettingsURLString)) {
                DC_OpenUrl(UIApplicationOpenSettingsURLString);
            }
        }];

        if (![DC_KeyWindow.subviews.lastObject isKindOfClass:[DCAlterView class]]) {
            [DC_KeyWindow addSubview:alterView];
            [alterView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(DC_KeyWindow);
            }];
        }
    }
}



#pragma mark - <CLLocationManagerDelegate>
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
{
    WEAKSELF;
    CLLocation * currLocation = [locations lastObject];
    NSLog(@"经度=%f 纬度=%f 高度=%f",currLocation.coordinate.latitude,currLocation.coordinate.longitude,currLocation.altitude);
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (placemarks.count > 0) {
            CLPlacemark * placemark = placemarks.firstObject;
            NSDictionary * address = [placemark addressDictionary];
            
            if (weakSelf.successBlock) {
                weakSelf.successBlock([address objectForKey:@"City"]);
            }
            
            if (weakSelf.nowBlock) {
                weakSelf.nowBlock(placemark.location.coordinate.longitude,placemark.location.coordinate.latitude);
            }
        }
        
        for (CLPlacemark * placemark in placemarks) {
            NSDictionary * address = [placemark addressDictionary];
            NSLog(@"####%@",address);
            NSLog(@"Country --- %@",[address objectForKey:@"Country"]);
            NSLog(@"State --- %@",[address objectForKey:@"State"]);
            NSLog(@"City --- %@",[address objectForKey:@"City"]);
            NSLog(@"SubLocality --- %@",[address objectForKey:@"SubLocality"]);
            weakSelf.locationManager = nil;
            weakSelf.locationManager.delegate = nil;
        }
    }];
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"定位失败 - %@",error);
    
    [SVProgressHUD showInfoWithStatus:@"定位失败，请确定您的定位权限是否开启"];
    
//    DCAlterView * alterView = [[DCAlterView alloc] initWithTitle:@"提示" content:@"定位失败，请确定您的定位权限是否开启"];
//    [alterView addActionWithTitle:@"确认" type:DCAlterTypeDone halderBlock:^(UIButton *button) {
//        // 打开定位设置
//        if (DC_CanOpenUrl(UIApplicationOpenSettingsURLString)) {
//            DC_OpenUrl(UIApplicationOpenSettingsURLString);
//        }
//    }];
    
//    [DC_KeyWindow addSubview:alterView];
//    [alterView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(DC_KeyWindow);
//    }];
    
//    if (![DC_KeyWindow.subviews.lastObject isKindOfClass:[DCAlterView class]]) {
//        [DC_KeyWindow addSubview:alterView];
//        [alterView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(DC_KeyWindow);
//        }];
//    }
}

@end
